package com.mer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mer.entity.TeacherExamine;
import com.mer.mapper.TeacherExamineMapper;
import com.mer.mapper.TeacherMapper;
import com.mer.service.TeacherService;
import com.mer.entity.Teacher;
import com.mer.vo.CommonResult;
import com.mer.vo.NewLecturerVo;
import com.mer.vo.TeacherExamineVo;
import com.mer.vo.TeacherVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @program: IIPhase-project
 * @description: 教师业务实现类
 * @author: 张庆恒
 * @create: 2021-10-19 09:22
 **/
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Resource
    private TeacherMapper teacherMapper;

    @Resource
    private TeacherExamineMapper teacherExamineMapper;
    //查询全部已经通过的教师
    @Override
    public CommonResult findTeacherAll(Integer page, Integer limit, TeacherVo teacherVo) {
        IPage<Teacher> iPage = new Page<>(page, limit);
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(teacherVo.getName())) {
            wrapper.like("name", teacherVo.getName());
        }
        if (StringUtils.isNotEmpty(teacherVo.getMobile())) {
            wrapper.like("mobile", teacherVo.getMobile());
        }
        if (StringUtils.isNotEmpty(teacherVo.getStatus())) {
            wrapper.like("status", teacherVo.getStatus());
        }
        IPage<Teacher> teacherIPage = teacherMapper.queryTeacherWithPass(iPage, wrapper);
        return new CommonResult(200, "查询成功", teacherIPage);
    }
    //修改教师状态
    @Override
    public CommonResult changeTeacherStatus(TeacherVo teacherVo) {
        Teacher teacher = new Teacher();
        if (StringUtils.isNotEmpty(teacherVo.getId())) {
            teacher.setId(teacherVo.getId());
        }
        if (StringUtils.isNotEmpty(teacherVo.getStatus())) {
            teacher.setStatus(teacherVo.getStatus());
        }
        int i = teacherMapper.updateById(teacher);
        if (i>0){
            return new CommonResult(200,"修改成功","yes");
        }else {
            return new CommonResult(500,"修改失败","no");
        }
    }
    //修改个人信息
    @Override
    public CommonResult updateTeacherInfo(TeacherVo teacherVo) {
        Teacher teacher = new Teacher();
        teacher.setId(teacherVo.getId());
        teacher.setName(teacherVo.getName());
        teacher.setIntro(teacherVo.getIntro());
        teacher.setMobile(teacherVo.getMobile());
        teacher.setEmail(teacherVo.getEmail());
        int i = teacherMapper.updateById(teacher);
        if (i>0){
            return new CommonResult(200,"修改成功","yes");
        }else {
            return new CommonResult(500,"修改失败","no");
        }
    }

    @Override
    public CommonResult addTeacher(NewLecturerVo newLecturerVo) {
        //判断手机号有没有被注册
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        Teacher teacher = new Teacher();
        wrapper.eq("mobile",newLecturerVo.getMobile());
        Teacher mobile = teacherMapper.selectOne(wrapper);
        if (mobile != null) {
            return new CommonResult(500,"该手机号已被注册",null);
        }else {
            QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
            teacher.setName(newLecturerVo.getName());
            teacher.setIntro(newLecturerVo.getIntro());
            teacher.setEmail(newLecturerVo.getEmail());
            //teacher.setGmtCreate(LocalDateTime.now());
            teacher.setAvatar(newLecturerVo.getAvatar());
            teacher.setLevel(newLecturerVo.getLevel());
            //teacher.setGmtModified(LocalDateTime.now());
            teacher.setMobile(newLecturerVo.getMobile());
            teacher.setIsDeleted(false);
            teacher.setStatus("0");
            teacher.setSort(0);
            int i = teacherMapper.insert(teacher);
            queryWrapper.eq("mobile",newLecturerVo.getMobile());
            Teacher teacher2 = teacherMapper.selectOne(queryWrapper);
            TeacherExamine teacherExamine = new TeacherExamine();
            teacherExamine.setTeacherId(teacher2.getId());
            teacherExamine.setCheckStatus("2");
            int ii = teacherExamineMapper.insert(teacherExamine);
            if (i>0 && ii>0){
                return new CommonResult(200,"添加成功",null);
            }else{
                return new CommonResult(500,"添加失败",null);
            }
        }
    }
}
